package org.anachronos.jeep.core.trace.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlRootElement;

import org.anachronos.jeep.core.trace.MethodDescriptionConverter;

@XmlRootElement
public class Trace {
	@XmlElementWrapper(name="methods")
	@XmlIDREF
	private List<Method> method = new ArrayList<Method>();
	
	@XmlElementWrapper(name="callStacks")
	private List<ThreadCallStack> callStack = new ArrayList<ThreadCallStack>();
	
	@XmlElementWrapper(name="mergedMethodInvocations")
	private List<MethodInvocation> mergedMethodInvocations = new ArrayList<MethodInvocation>();

	@XmlElementWrapper(name="mergedSqlStatementExecutions")
	private List<SqlStatementExecution> mergedSqlStatementExecutions = new ArrayList<SqlStatementExecution>();

	@XmlElement
	private ThreadCallStack mergedCallStack = new ThreadCallStack("Merged Call Stack", -1);
	
	@XmlElement
	private Package rootPackage = new Package("", "root");
	
	private transient Map<String, Method> idToMethod = 
		new HashMap<String, Method>();
	
	private transient Map<Long, ThreadCallStack> threadIdToCallStack = 
		new HashMap<Long, ThreadCallStack>();
	
	private transient Map<Method, MethodInvocation> profiledMethodToMergedMethodInvocation = 
		new HashMap<Method, MethodInvocation>();

	
	private transient Map<Object, SqlStatementExecution> sqlStatementExecutionToMergedSqlStatementExecution = 
		new HashMap<Object, SqlStatementExecution>();

	/**
	 * Required by JAXB.
	 */
	public Trace() {	
	}

	public void addToMergeMethodInvocations(final MethodInvocation methodInvocation) {
		final MethodInvocation mergedMethodInvocation;
		final Method profiledMethod = methodInvocation.getMethod();
		if (!profiledMethodToMergedMethodInvocation.containsKey(profiledMethod)) {
			mergedMethodInvocation = new MethodInvocation();
			mergedMethodInvocation.setMethod(methodInvocation.getMethod());
			profiledMethodToMergedMethodInvocation.put(profiledMethod, mergedMethodInvocation);
			mergedMethodInvocations.add(mergedMethodInvocation);
		} else {
			mergedMethodInvocation = profiledMethodToMergedMethodInvocation.get(profiledMethod);
		}
		mergedMethodInvocation.getMetrics().add(methodInvocation.getMetrics());
		mergedMethodInvocation.getSelfMetrics().add(methodInvocation.getSelfMetrics());
	}
	
	public void addToMergedSqlStatementExecutions(final SqlStatementExecution sqlStatementExecution) {
		final SqlStatementExecution mergedSqlStatementExecution;
		if (!sqlStatementExecutionToMergedSqlStatementExecution.containsKey(sqlStatementExecution.id())) {
			mergedSqlStatementExecution = new SqlStatementExecution(sqlStatementExecution.getClassName(), 
					sqlStatementExecution.getStatement());
			sqlStatementExecutionToMergedSqlStatementExecution.put(sqlStatementExecution.id(), 
					mergedSqlStatementExecution);
			mergedSqlStatementExecutions.add(mergedSqlStatementExecution);
		} else {
			mergedSqlStatementExecution = sqlStatementExecutionToMergedSqlStatementExecution.get(sqlStatementExecution.id());
		}
		final Metrics mergedMetrics = mergedSqlStatementExecution.getMetrics();
		mergedMetrics.add(sqlStatementExecution.getMetrics());
	}
	
	public ThreadCallStack getMergedCallStack() {
		return mergedCallStack;
	}
	
	public List<ThreadCallStack> getThreadCallStacks() {
		return Collections.unmodifiableList(callStack);
	}
	
	public ThreadCallStack getCurrentThreadCallStack() {
		Thread currentThread = Thread.currentThread();
		long id = currentThread.getId();
		ThreadCallStack threadCallStack = threadIdToCallStack.get(id);
		if (threadCallStack == null) {
			threadCallStack = new ThreadCallStack(currentThread.getName(), id);
			threadIdToCallStack.put(id, threadCallStack);
			callStack.add(threadCallStack);
		}
		return threadCallStack;
	}
	
	public void addProfiledMethod(final String methodDesc) {
		if (!idToMethod.containsKey(methodDesc)) {
			final Method profiledMethod = MethodDescriptionConverter.createAndAddToRoot(rootPackage, methodDesc);
			method.add(profiledMethod);
			putMethodToIdMap(profiledMethod);
		}
	}

	public void putMethodToIdMap(final Method method) {
		idToMethod.put(method.getId(), method);
	}

	public Method getMethod(final String id) {
		final Method result = idToMethod.get(id);
		return result;
	}
	
	public List<Method> getMethods() {
		return method;
	}

	public void accept(ModelVisitor visitor) {
		visitor.visit(this);
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((callStack == null) ? 0 : callStack.hashCode());
		result = prime * result + ((method == null) ? 0 : method.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Trace other = (Trace) obj;
		if (callStack == null) {
			if (other.callStack != null)
				return false;
		} else if (!callStack.equals(other.callStack))
			return false;
		if (method == null) {
			if (other.method != null)
				return false;
		} else if (!method.equals(other.method))
			return false;
		return true;
	}
}
